import "bootstrap/dist/css/bootstrap.min.css";
import { Modal } from "bootstrap";
import axios from "axios";
export default {
    data() {
        return {
            dropdownActive: false,
            oldPassword: "",
            newPassword: "",
            changePasswordModal: null,
            subMenu: "", // 新增subMenu属性
            welcomeVisible: true, // 新增 welcomeVisible 属性
        };
    },

    computed: {
        loginUsername() {
            return localStorage.getItem("username");
        },
    },

    methods: {
        toggleUserOptions(event) {
            event.stopPropagation();
            this.dropdownActive = !this.dropdownActive;
        },

        handleLogout() {
            // Your logout logic here
            this.$router.push({ name: "Login" });
        },
        //打开子导航
        toggleSubMenu(menu) {
            this.subMenu = this.subMenu === menu ? "" : menu;
        },
        //打开模态框
        openModal() {
            this.changePasswordModal.show();
        },

        closeModal() {
            this.changePasswordModal.hide();
        },
        //修改密码
        async changePassword() {
            try {
                const url = "http://localhost:8081/admin/change";

                // 获取令牌
                const token = localStorage.getItem("token");

                const requestData = {
                    oldPwd: this.oldPassword,
                    newPwd: this.newPassword,
                };

                const requestHeaders = {
                    "Authorization": `${token}`,
                };

                const response = await axios.put(url, requestData, { headers: requestHeaders });

                if (response.data.code === 20000) {
                    this.$message.success("密码修改成功！");
                } else {
                    this.$message.error(response.data.message);
                }
            } catch (error) {
                console.error("更改密码失败:", error);
                this.$message.error("更改密码失败，请稍后重试。");
            }
            this.closeModal();
        },

    },

    mounted() {
        this.changePasswordModal = new Modal(document.getElementById("changePasswordModal"));
    },
};